package com.servlet;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class testServlet1
 */
@WebServlet("/sessionServlet")
public class sessionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public sessionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession(true);
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		
		session.setAttribute("name", name);
		session.setAttribute("pwd", password);
		
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out=response.getWriter();
		
		if(session.isNew()){
			session.setAttribute("count", 0);
		}else{
			int visitCount=(int) session.getAttribute("count");
			visitCount+=1;
			session.setAttribute("count", visitCount);
			
		}
		out.write("<table border='1'><tr><td>"+session.getId()+"</td><td>"+session.getAttribute("name")
				+"</td><td>"+session.getAttribute("pwd")+"</td><td>"+session.getAttribute("count")
				+"</td></tr></table>");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
